package nl.fotoniq.android.lockpicker.shared;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

/**
 * The Class LockPickerNotifier.
 * <p>
 * Displays notifications either in the device status bar or as toast messages.
 * </p>
 * 
 * @author jeroen.brosens
 */
public class LockPickerNotifier {

	private static final int NOTIFY_ID = 0;

	/**
	 * Show notification.
	 * 
	 * @param context
	 *            the context
	 * @param iconId
	 *            the icon id
	 * @param contentTitle
	 *            the content title
	 * @param tickerText
	 *            the ticker text
	 * @param contentText
	 *            the content text
	 */
	public static void showNotification(final Context context, final int iconId, final String contentTitle,
			final String tickerText, final String contentText) {

		// determine whether a notification should be shown
		SharedPreferences preferences = context.getSharedPreferences(LockPickerConstants.PREFERENCES_NAME,
				Context.MODE_PRIVATE);

		boolean isShowingNotifications = preferences
				.getBoolean(context.getString(R.string.pref_show_notifications_key),
						LockPickerConstants.DEFAULT_SHOW_NOTIFICATIONS);

		LockPickerUtil.log_d("Is showing notifications: " + isShowingNotifications);

		// show notification if applicable
		if (isShowingNotifications) {

			// Get a reference to the NotificationManager
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);

			// Instantiate the Notification
			Notification notification = new Notification(iconId, tickerText, System.currentTimeMillis());

			// Define the Notification's expanded message and Intent
			Intent notificationIntent;

			notificationIntent = new Intent(context, LockPickerUtil.getApplicationMainActivityClass(context));
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
					Intent.FLAG_ACTIVITY_NEW_TASK);

			notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

			LockPickerUtil.log_d("Dispatching notification");

			notificationManager.notify(NOTIFY_ID, notification);
		}
	}

	/**
	 * Cancel notification.
	 * 
	 * @param context
	 *            the context
	 */
	public static void cancelNotification(final Context context) {
		// Get a reference to the NotificationManager
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);

		LockPickerUtil.log_d("Cancelling notification");

		notificationManager.cancel(NOTIFY_ID);
	}

	/**
	 * Show toast.
	 * 
	 * @param context
	 *            the context
	 * @param resId
	 *            the res id
	 */
	public static void showToast(final Context context, final int resId) {
		Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
	}
}
